<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">

      <!-- 考试详情自定义 -->
      <template slot-scope="scope" slot="menu">
        <el-button type="primary" size="small"  @click="handleExamDetail(scope.row)">考试详情</el-button>
      </template>
    </avue-crud>

    <!--考试详情弹窗-->
    <el-dialog title="考试 详情" :visible.sync="dialogExamDetail" append-to-body @close='closeDialogExamDetail' width="1200px" class="prodUnitXjd">
      <avue-crud :option=" examDetailOption"
                 :table-loading="examDetailLoading"
                 :data="examDetailData"
                 :page.sync="examDetailPage"
                 ref="examDetailCrud"
                 @search-change="examDetailSearchChange"
                 @search-reset="examDetailSearchReset"
                 @selection-change="examDetailSelectionChange"
                 @current-change="examDetailCurrentChange"
                 @size-change="examDetailSizeChange">
      </avue-crud>
      <span slot="footer" class="dialog-footer">
          <el-button @click="closeDialogExamDetail">取 消</el-button>
        </span>
    </el-dialog>

  </basic-container>
</template>

<script>
  import {getList, getExamDetail} from "@/api/jypx/edutraincount/traincount";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "员工姓名",
              prop: "userName",
              align: "center",
              search: true,
              slot: false,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },

            {
              label: "累计学习时间",
              prop: "ljxxsj",
              align: "center",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "考试次数",
              prop: "kscs",
              align: "center",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "最近学习时间",
              prop: "zjxxsj",
              align: "center",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "最近考试时间",
              prop: "zjkssj",
              align: "center",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
          ]
        },
        data: [],

        examDetailOption: {
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          menu: false,
          addBtn: false,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          refreshBtn: false,
          columnBtn: false,
          column: [
            {
              label: "员工姓名",
              prop: "ksrName",
              align: "center",
            },
            {
              label: "课程名称",
              prop: "courseId",
              align: "center",
              dicUrl: "/api/eduCourse/course/getCourseKcmc",
              props: {
                label: "kcmc",
                value: "id",
              },
            },
            {
              label: "考试时间",
              prop: "ksTime",
              align: "center",
            },
            {
              label: "考试成绩",
              prop: "kscj",
              align: "center",
            },
            {
              label: "考试结果",
              prop: "ksjg",
              align: "center",
              search: true,
              slot: false,
              type:"select",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=type_of_examrecord",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              dataType: "number",
            },
            {
              label: "用时(分钟)",
              prop: "dtsc",
              align: "center",
            },
          ]
        },
        examDetailQuery: {},
        dialogExamDetail: false,
        examDetailLoading: true,
        examDetailSelectionList: [],
        examDetailPage: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        examDetailData: [],
        selectExamDetail: {},
        currentId: null
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.studyrecord_add, false),
          viewBtn: this.vaildData(this.permission.studyrecord_view, false),
          delBtn: this.vaildData(this.permission.studyrecord_delete, false),
          editBtn: this.vaildData(this.permission.studyrecord_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      //打开考试详情弹窗
      handleExamDetail(row){
        this.dialogExamDetail = true;
        this.currentId = row.id;
        this.examDetailOnLoad(this.examDetailPage);
      },

      //关闭考试详情弹窗
      closeDialogExamDetail(){
        this.examDetailPage.currentPage = 1;
        this.dialogExamDetail = false;
        this.examDetailSelectionClear();
      },
      //考试详情弹窗清空勾选
      examDetailSelectionClear(){
        this.examDetailSelectionList = [];
        this.$refs.examDetailCrud.toggleSelection();
      },

      //考试详情获取表格数据
      examDetailOnLoad(page, params={}){
        this.examDetailLoading = true;
        params.id = this.currentId;
        getExamDetail(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.examDetailPage.total = data.total;
          this.examDetailData = data.records;
          this.examDetailLoading = false;
          if(this.selectExamDetail !== undefined && JSON.stringify(this.selectExamDetail) !== "{}") {
            this.$nextTick(() => {
              this.examDetailData.forEach(row => {
                if(this.selectExamDetail[row.id] !== undefined) {
                  this.$refs.examDetailCrud.toggleRowSelection(row, true);
                }
              })
            })
          }
        });
      },

      //考试详情搜索条件改变事件
      examDetailSearchChange(params, done){
        this.examDetailQuery = params;
        this.examDetailPage.currentPage = 1;
        this.examDetailOnLoad(this.examDetailPage, params);
        done();
      },

      //考试详情清空搜索条件
      examDetailSearchReset(){
        this.examDetailQuery = {};
        this.examDetailOnLoad(this.examDetailPage);
      },

      //考试详情勾选
      examDetailSelectionChange(list){
        this.examDetailSelectionList = list;
      },

      //考试详情换页数
      examDetailCurrentChange(currentPage){
        const pageSelectIds = [];
        const pageAllIds = [];
        this.examDetailSelectionList.forEach(item => {
          this.selectExamDetail[item.id] = item;
          pageSelectIds.push(item.id);
        });
        this.examDetailData.forEach(item => {
          pageAllIds.push(item.id);
        });
        for(let id in this.selectExamDetail){
          if(pageSelectIds.indexOf(id) < 0 && pageAllIds.indexOf(id) >= 0){
             delete this.selectExamDetail[id];
          }
        }
        this.examDetailPage.currentPage = currentPage;
        this.examDetailOnLoad(this.examDetailPage);
      },

      //显示数量
      examDetailSizeChange(pageSize){
        this.examDetailPage.pageSize = pageSize;
        this.examDetailOnLoad(this.examDetailPage);
      },

      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
